<?php

class ShopCTR extends Controller
{
    private $config;
    function init()
    {
        $ConfigModel = new Model('config');
        $config = $ConfigModel->fetch();
        $this->config = $config;
        $this->assign('config', $config);
    }

    function vip()
    {
        if ($login_user = Session::get('user')) {
            if ($this->isPost()) {
                $time = ($_POST['time'] + 0) * 2592000;
                $UserModel = new Model('user');
                $user = $UserModel->fetch('*', array('id' => $login_user['id']));
                $user_time = empty($user['vip']) ? '0' : $user['vip'];
                $now_time = time();
                if ($user_time < $now_time) {
                    //已经到期
                    $update_time = $now_time + $time;
                } else {
                    //没有到期，合并式增加时间
                    $update_time = $user_time + $time;
                }
                if ($time) {
                    if ($UserModel->update(array('vip' => $update_time), array('id' => $login_user['id']))) {
                        $this->success('购买成功', $this->urlCreate('user', 'home'));
                    } else {
                        $this->error('系统出错，购买失败', 'javascript:history.go(-1);');
                    }
                } else {
                    $this->error('购买时长错误，无法购买', 'javascript:history.go(-1);');
                 }
            }
            $this->assign('title', '购买VIP - ' . $this->config['name']);
            $this->display();
        } else {
            $this->error('您还未登录', $this->urlCreate('user', 'login'));
        }
    }
}

?>